﻿using com.iot.core.annotation; 
using com.iot.core.data; 
using com.iot.core.infrastructure;  
using com.iot.ht.Controllers.htsoft.service;
using com.iot.ht.Controllers.htuser.data; 
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using com.iot.core.ext;
using Icom.iot.core.ext;
using com.iot.ht.Controllers.htsoft.data;
using com.iot.core.manager.id;
using Microsoft.EntityFrameworkCore.Query;

namespace com.iot.ht.Controllers.htsoft
{
    [ApiController]
    [Route("[controller]/[action]")]
    [Tag(Name = "软件监控", Description = "")]
    public class HtSoftController : ControllerBase
    {
        readonly IRepository<HtSoftLog> htSoftLogMapper;
        public HtSoftController(IRepository<HtSoftLog> p1)
        {
            htSoftLogMapper = p1;   
        }
        [HttpPost]
        [Operation(Summary = "软件监控分页", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<Page<HtSoftAppStateDto>> PageAsync([FromBody] BasePageQto mm)
        {
            var l = Singleton<HtSoftService>.Instance.Apps;
            if (!string.IsNullOrWhiteSpace(mm.Key))
            {
                l=l.Where(a=>a.Name.Contains(mm.Key)).ToList();
            }
            var r = new Page<HtSoftAppStateDto>(l.Count, mm.Page, mm.PageSize);
            r.Records=l.AsQueryable().SortPageTake(new Sort(),r);
            return r;
        }

        [HttpPost]
        [Operation(Summary = "软件监控日志分页", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<Page<HtSoftLog>> PageLogAsync([FromBody] BasePageQto mm)
        {
            var q = htSoftLogMapper.GetAll();
            if (mm.Id > 0)
            {
                q = q.Where(a => a.HtsoftappId == mm.Id);
            }
            if (!string.IsNullOrEmpty(mm.Key))
            {
                q = q.Where(a => a.Msg.Contains(mm.Key));
            }
            var r = await htSoftLogMapper.PageAsync(q, mm);
            return r;
        }


        [HttpPost]
        [Operation(Summary = "软件详情", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public async Task<HtSoftApp> DetailAsync(BaseQto qm)
        {
            var l = Singleton<HtSoftService>.Instance.Apps;
            var m = l.Where(a => a.Id == qm.Id).FirstOrDefault();
            return m.HtSoftApp;
        }

        [HttpPost]
        [Operation(Summary = "插入初始化", Description = "")]
        [Auth(AuthType.Login)]
        [Log(LogNeed = false, LogPin = false, LogPout = false, LogTemplete = "")]
        public HtSoftApp InsertInit()
        {
            var Sortno = Singleton<HtSoftService>.Instance.HtSoftApps.Max(a => (long?)a.Sortno);

            var mm = new HtSoftApp
            {
                Id = HtIdManager.Current.NextLong(),
                Lx = "Exe",
                CkMethod = "Http",
                Sortno=10,
                AutoStart=true,
                StartWaitMs=10000
            };
            if (Sortno.HasValue)
            {
                mm.Sortno = Sortno.Value + 10;
            }
            return mm;
        }


        [HttpPost]
        [Operation(Summary = "添加监控软件", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Insert(HtSoftApp mm)
        {
            var r = Singleton<HtSoftService>.Instance.Insert(mm);
            return r;
        }

        [HttpPost]
        [Operation(Summary = "修改监控软件", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Update(HtSoftApp mm)
        {
            var r = Singleton<HtSoftService>.Instance.Update(mm);
            return r;
        }

        [HttpPost]
        [Operation(Summary = "删除监控软件", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Delete(BaseQto mm)
        {
            var r = Singleton<HtSoftService>.Instance.Delete(mm);
            return r;
        }


        [HttpPost]
        [Operation(Summary = "启动监控软件", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public async  Task<int>    Start(BaseQto mm)
        {
            var r =await Singleton<HtSoftService>.Instance.StartSingleApp(mm);
            return r;
        }

        [HttpPost]
        [Operation(Summary = "停止监控软件", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int Stop(BaseQto mm)
        {
            var r = Singleton<HtSoftService>.Instance.StopSingleApp(mm);
            return r;
        }

        [HttpPost]
        [Operation(Summary = "关闭监控软件", Description = "")]
        [Auth(AuthType.Auth)]
        [Log(LogNeed = true, LogPin = true, LogPout = true, LogTemplete = "")]
        public int StopEx(BaseQto mm)
        {
            var r = Singleton<HtSoftService>.Instance.StopSingleAppEx(mm);
            return r;
        }

    }
}
